<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The SplFixedArray class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="splpriorityqueue.valid.html">« SplPriorityQueue::valid</a></li>
      <li style="float: right;"><a href="splfixedarray.construct.html">SplFixedArray::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="spl.datastructures.html">数据结构</a></li>
    <li>The SplFixedArray class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.splfixedarray" class="reference">
 <h1 class="title">The SplFixedArray class</h1>
 
 
 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p>
 

  <div class="section" id="splfixedarray.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    The SplFixedArray class provides the main functionalities of array. The 
    main difference between a SplFixedArray and a normal PHP array is that 
    the SplFixedArray must be resized manually and allows only integers within 
    the range as indexes. The advantage is that it uses less memory than
    a standard <span class="type">array</span>.
   </p>
  </div>

 
  <div class="section" id="splfixedarray.synopsis">
   <h2 class="title">类摘要</h2>
 

   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">SplFixedArray</strong>
     </span>

     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.iteratoraggregate.html" class="interfacename">IteratorAggregate</a></span><span class="oointerface">,  <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span><span class="oointerface">,  <a href="class.countable.html" class="interfacename">Countable</a></span><span class="oointerface">,  <a href="class.jsonserializable.html" class="interfacename">JsonSerializable</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.construct.html" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code><span class="initializer"> = 0</span></span>)</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.count.html" class="methodname">count</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.current.html" class="methodname">current</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="splfixedarray.fromarray.html" class="methodname">fromArray</a></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserveKeys</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type"><a href="class.splfixedarray.html" class="type SplFixedArray">SplFixedArray</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.getsize.html" class="methodname">getSize</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.key.html" class="methodname">key</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.next.html" class="methodname">next</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.offsetexists.html" class="methodname">offsetExists</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.offsetget.html" class="methodname">offsetGet</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.offsetset.html" class="methodname">offsetSet</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.offsetunset.html" class="methodname">offsetUnset</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.rewind.html" class="methodname">rewind</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.setsize.html" class="methodname">setSize</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.toarray.html" class="methodname">toArray</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.valid.html" class="methodname">valid</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splfixedarray.wakeup.html" class="methodname">__wakeup</a></span>(): <span class="type"><span class="type void">void</span></span></div>

   }</div>
 
  </div>

  <div class="section">
   <h2 class="title">更新日志</h2>
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.1.0</td>
       <td>
        <span class="classname"><strong class="classname">SplFixedArray</strong></span> implements
        <span class="interfacename"><a href="class.jsonserializable.html" class="interfacename">JsonSerializable</a></span> now.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        <span class="classname"><strong class="classname">SplFixedArray</strong></span> implements
        <span class="interfacename"><a href="class.iteratoraggregate.html" class="interfacename">IteratorAggregate</a></span> now.
        Previously, <span class="interfacename"><a href="class.iterator.html" class="interfacename">Iterator</a></span> was implemented instead.
       </td>
      </tr>

     </tbody>
    
   </table>

  </div>


  <div class="section" id="splfixedarray.examples">
   <h2 class="title">范例</h2>
   <p class="para">
    <div class="example" id="example-3545">
     <p><strong>示例 #1 <span class="classname"><strong class="classname">SplFixedArray</strong></span> usage example</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Initialize&nbsp;the&nbsp;array&nbsp;with&nbsp;a&nbsp;fixed&nbsp;length<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFixedArray</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">4</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;int(2)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"4"</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;string(3)&nbsp;"foo"<br /><br />//&nbsp;Increase&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;array&nbsp;to&nbsp;10<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setSize</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">9</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"asdf"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Shrink&nbsp;the&nbsp;array&nbsp;to&nbsp;a&nbsp;size&nbsp;of&nbsp;2<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setSize</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;following&nbsp;lines&nbsp;throw&nbsp;a&nbsp;RuntimeException:&nbsp;Index&nbsp;invalid&nbsp;or&nbsp;out&nbsp;of&nbsp;range<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"non-numeric"</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[-</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]);<br />}&nbsp;catch(</span><span style="color: #0000BB">RuntimeException&nbsp;$re</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"RuntimeException:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$re</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">().</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
NULL
int(2)
string(3) &quot;foo&quot;
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
</pre></div>
     </div>
    </div>
   </p>
  </div>

 

 
 </div>
 
 































































































 
<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="splfixedarray.construct.html">SplFixedArray::__construct</a> — Constructs a new fixed array</li><li><a href="splfixedarray.count.html">SplFixedArray::count</a> — Returns the size of the array</li><li><a href="splfixedarray.current.html">SplFixedArray::current</a> — Return current array entry</li><li><a href="splfixedarray.fromarray.html">SplFixedArray::fromArray</a> — Import a PHP array in a SplFixedArray instance</li><li><a href="splfixedarray.getsize.html">SplFixedArray::getSize</a> — Gets the size of the array</li><li><a href="splfixedarray.key.html">SplFixedArray::key</a> — Return current array index</li><li><a href="splfixedarray.next.html">SplFixedArray::next</a> — Move to next entry</li><li><a href="splfixedarray.offsetexists.html">SplFixedArray::offsetExists</a> — Returns whether the requested index exists</li><li><a href="splfixedarray.offsetget.html">SplFixedArray::offsetGet</a> — Returns the value at the specified index</li><li><a href="splfixedarray.offsetset.html">SplFixedArray::offsetSet</a> — Sets a new value at a specified index</li><li><a href="splfixedarray.offsetunset.html">SplFixedArray::offsetUnset</a> — Unsets the value at the specified $index</li><li><a href="splfixedarray.rewind.html">SplFixedArray::rewind</a> — Rewind iterator back to the start</li><li><a href="splfixedarray.setsize.html">SplFixedArray::setSize</a> — Change the size of an array</li><li><a href="splfixedarray.toarray.html">SplFixedArray::toArray</a> — Returns a PHP array from the fixed array</li><li><a href="splfixedarray.valid.html">SplFixedArray::valid</a> — Check whether the array contains more elements</li><li><a href="splfixedarray.wakeup.html">SplFixedArray::__wakeup</a> — Reinitialises the array after being unserialised</li></ul>
</div>
</div></div></body></html>